<?
// $_REQUEST= $_POST + $_GET
ini_set('include_path', '/home/tadams/classes/');
require_once('ri/converter/ConverterFactory.php');
require_once('ri/validator/ValidatorFactory.php');

$converter= ConverterFactory::converterForName('Date')
	->setDefault(new DateTime())
	->setErrorMessage('Invalid date.')
	->setFormat('%d/%m/%Y');

$validator= ValidatorFactory::validatorForName('DateRange')
	->setErrorMessage('Date is in the past.')
	->setMin(new DateTime());

if (!array_key_exists('date',$_REQUEST) || is_null($_REQUEST['date']))
	$_REQUEST['date']= $converter->getAsString($converter->getDefault());

// default value
$errors= array();
if (isset($_REQUEST['submitted'])) {
	// conversion
	try {
		$date= $converter->getAsDatatype($_REQUEST['date']);
		//validation
		$validator->validate($date);
		// formatting
		echo('Sent date: '.$date->format('d/m/Y'));
	}
	// error message comes from the exception
	catch (ConverterException $e) {
		$errors[]= $e->getErrorMessage(); 
	}
	catch (ValidatorException $e) {
		$errors[]= $e->getErrorMessage(); 
	}
}

// INPUT mask
?>
<h2>Please enter following fields</h2>

<ul>
<? foreach ($errors as $error) { ?>
	<li><?= $error; ?></li>
<? } ?>
</ul>

<form>
	<input type="hidden" name="submitted" value="true" />
	<span>Date:</span>
	<input type="text" name="date" value="<?= $_REQUEST['date']; ?>" />
	<input type="submit" value="Submit" />
</form>
